package capricornus.components.utils.richtext.cmd
{
	import capricornus.utils.Base64;
	import capricornus.utils.StringUtils;
	
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.Dictionary;
	
	public class RichTextCmdAnalyzer
	{
		private static var _ins:RichTextCmdAnalyzer=null;
		
		private var _dictCMDAnalyseFun:Dictionary;
		private var _dictCMDHandleFun:Dictionary;
		
		public function RichTextCmdAnalyzer()
		{
			init();
		}
		
		private static function get instance():RichTextCmdAnalyzer
		{
			if( null==_ins )
			{
				_ins = new RichTextCmdAnalyzer();
			}
			return _ins;
		}
		
		private function init():void
		{
			_dictCMDAnalyseFun = new Dictionary();
			_dictCMDHandleFun = new Dictionary();
			
			//"ig"图片  "ul"网页链接
			/*[ig=0208916249da124531c0f7d00e10d362ede6]截图[/ig]
			[ul=aHR0cDovL3d3dy5iYWlkdS5jb20=]百度[/ul] aHR0cDovL3d3dy5iYWlkdS5jb20=是经过Base64编码过的字符串*/
			_dictCMDAnalyseFun["ig"] = 1;
			_dictCMDHandleFun["ig"] = handleScreenShot;
			
			_dictCMDAnalyseFun["ul"] = 1;
			_dictCMDHandleFun["ul"] = handleUrl;
			
			//"us"是用户资料链接             用户ID + 空格 + 用户名
			//示例：[us=21423 玩家名]玩家名[/us]
			_dictCMDAnalyseFun["us"] = 1;
			_dictCMDHandleFun["us"] = handleUserInfo;
			
			//"jp"是场景跳转             场景ID + (空格 + 跳转X + 空格 + 跳转Y)
			//括号部分可以要可以不要
			//[jp=21423]场景名[/jp]
			_dictCMDAnalyseFun["jp"] = 1;
			_dictCMDHandleFun["jp"] = handleSceneJump;
		}
		
		public function analyse( str:String ):String
		{
			var len:int = str.length;
			var start:int = 0;
			var index:int;
			var cmd:String = "";
			var cmdStrLen:int = 1;
			var arr:Array;
			while( true )
			{
				cmdStrLen = 1;
				index = str.indexOf( "[", start );
				if( index==-1 )
				{
					break;
				}
				//不够命令字长度
				if( index+2 >= len ) break;
				
				cmd = str.substr( index+1, 2 );
				if( _dictCMDAnalyseFun[cmd] )
				{
					arr = analyseCMD( str, cmd, index );
					if( arr[0] )
					{
						str = arr[1];
						len = str.length;
					}
				}
				
				start = index + 1;
			}
			return str;
		}
		
		public function handle( str:String ):void
		{
			var cmd:String = str.substr( 0, 2 );
			var fun:Function = _dictCMDHandleFun[cmd];
			if( fun != null )
			{
				if( str.charAt(2)=="=" )
				{
					fun( str.substr( 3 ) );
				}
			}
		}
		
		private var _color:uint = 0xff0000;
		private function analyseCMD( str:String, cmd:String, start:int ):Array
		{
			var index:int = str.indexOf( "[", start+1 );
			var cmdStr:String = "";
			var ok:Boolean = false;
			if( index != -1 )
			{
				cmd = "/"+cmd+"]";
				if( str.substr( index+1, 4 )==cmd )
				{
					cmdStr = str.substring( start, index+5 );
					var r:RegExp = /\[(.*)=(.+)\](.*)\[\/\1]/
					var arr:Array = cmdStr.match( r );
					if( arr != null )
					{
						var s:String = arr[3];
						if( s != "" && s != null )
						{
							ok = true;
							str = str.substr( 0, start ) + StringUtils.getLinkText( _color, arr[1], arr[2], s ) + str.substr( index+5 );
						}
					}
				}
			}
			
			return [ok,str];
		}
		
		private function handleScreenShot( str:String ):void
		{
			if( str=="" || str==null ) return;
			
//			PicLookupManager.instance.lookupScreenshot( str );
		}
		
		private function handleUserInfo( str:String ):void
		{
			if( str=="" || str==null ) return;
			
			var arr:Array = str.split( " " );
			if( arr.length==2 )
			{
//				PlayerProfileManager.show( arr[0], arr[1] );
			}
		}
		
		private function handleUrl( str:String ):void
		{
			if( str=="" || str==null ) return;
			
			var url:String = Base64.decode( str );
			navigateToURL( new URLRequest(url), "_blank" );
		}
		
		private function handleSceneJump( str:String ):void
		{
			if( str=="" || str==null ) return;
			var arr:Array = str.split( " " );
			var pt:Point = null;
			if( arr.length > 1 )
			{
				pt = new Point( Number(arr[1]), Number(arr[2]) );
			}
//			ActionSceneManager.instance.requestJumpTo( arr[0], pt );
		}
		
		/**
		 * 解析文本将里面的命令串转化成html 
		 * @param str
		 * @return 
		 * 
		 */		
		public static function analyse( str:String ):String
		{
			return instance.analyse( str );
		}
		
		/**
		 * 处理命令串的link文本 
		 * @param str 通过TextEvent.LINK事件传过来的text
		 * 
		 */		
		public static function handle( str:String ):void
		{
			instance.handle( str );
		}
		
		public static function createUserInfoFormat( userID:String, userName:String, color:uint=0xff0000 ):String
		{
			//[us=21423 玩家名]玩家名[/us]
			var str:String = "[us="+userID + " " + userName + "]"+userName+"[/us]";
			
			var old:uint = instance._color;
			instance._color = color;
			str = instance.analyse( str );
			instance._color = old;
			
			return str;
		}
	}
}








